{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>bathrooms</th>\n",
       "      <th>bedrooms</th>\n",
       "      <th>price</th>\n",
       "      <th>price_bathrooms</th>\n",
       "      <th>price_bedrooms</th>\n",
       "      <th>room_diff</th>\n",
       "      <th>room_num</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "      <th>Day</th>\n",
       "      <th>...</th>\n",
       "      <th>walk</th>\n",
       "      <th>walls</th>\n",
       "      <th>war</th>\n",
       "      <th>washer</th>\n",
       "      <th>water</th>\n",
       "      <th>wheelchair</th>\n",
       "      <th>wifi</th>\n",
       "      <th>windows</th>\n",
       "      <th>work</th>\n",
       "      <th>interest_level</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.5</td>\n",
       "      <td>3</td>\n",
       "      <td>3000</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>750.000000</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>4.5</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5465</td>\n",
       "      <td>2732.5</td>\n",
       "      <td>1821.666667</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>6</td>\n",
       "      <td>12</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2850</td>\n",
       "      <td>1425.0</td>\n",
       "      <td>1425.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>4</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3275</td>\n",
       "      <td>1637.5</td>\n",
       "      <td>1637.500000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>4</td>\n",
       "      <td>18</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3350</td>\n",
       "      <td>1675.0</td>\n",
       "      <td>670.000000</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>4</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 228 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   bathrooms  bedrooms  price  price_bathrooms  price_bedrooms  room_diff  \\\n",
       "0        1.5         3   3000           1200.0      750.000000       -1.5   \n",
       "1        1.0         2   5465           2732.5     1821.666667       -1.0   \n",
       "2        1.0         1   2850           1425.0     1425.000000        0.0   \n",
       "3        1.0         1   3275           1637.5     1637.500000        0.0   \n",
       "4        1.0         4   3350           1675.0      670.000000       -3.0   \n",
       "\n",
       "   room_num  Year  Month  Day       ...        walk  walls  war  washer  \\\n",
       "0       4.5  2016      6   24       ...           0      0    0       0   \n",
       "1       3.0  2016      6   12       ...           0      0    0       0   \n",
       "2       2.0  2016      4   17       ...           0      0    0       0   \n",
       "3       2.0  2016      4   18       ...           0      0    0       0   \n",
       "4       5.0  2016      4   28       ...           0      0    1       0   \n",
       "\n",
       "   water  wheelchair  wifi  windows  work  interest_level  \n",
       "0      0           0     0        0     0               1  \n",
       "1      0           0     0        0     0               2  \n",
       "2      0           0     0        0     0               0  \n",
       "3      0           0     0        0     0               2  \n",
       "4      0           0     0        0     0               2  \n",
       "\n",
       "[5 rows x 228 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读入数据\n",
    "dpath = './data/'\n",
    "train = pd.read_csv(dpath+\"RentListingInquries_FE_train.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_train = train['interest_level']\n",
    "train = train.drop([\"interest_level\"], axis=1)\n",
    "X_train = np.array(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 准备交叉验证\n",
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def modelfit(alg, X_train, y_train, useTrainCV=True, cv_folds=None, early_stopping_rounds=100):\n",
    "    \n",
    "    if useTrainCV:\n",
    "        xgb_param = alg.get_xgb_params()\n",
    "        xgb_param['num_class'] = 3\n",
    "        \n",
    "        xgtrain = xgb.DMatrix(X_train, label = y_train)\n",
    "        \n",
    "        cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], folds =cv_folds,\n",
    "                         metrics='mlogloss', early_stopping_rounds=early_stopping_rounds)\n",
    "        \n",
    "        n_estimators = cvresult.shape[0]\n",
    "        alg.set_params(n_estimators = n_estimators)\n",
    "        print (cvresult)\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     test-mlogloss-mean  test-mlogloss-std  train-mlogloss-mean  \\\n",
      "0              1.039621           0.000524             1.038716   \n",
      "1              0.990346           0.000496             0.988575   \n",
      "2              0.947454           0.000551             0.944989   \n",
      "3              0.910561           0.000479             0.907368   \n",
      "4              0.879129           0.000559             0.875143   \n",
      "5              0.851027           0.000882             0.846357   \n",
      "6              0.827000           0.001390             0.821581   \n",
      "7              0.805784           0.001753             0.799607   \n",
      "8              0.787173           0.001830             0.780226   \n",
      "9              0.770811           0.001915             0.763130   \n",
      "10             0.756050           0.001927             0.747635   \n",
      "11             0.742794           0.001954             0.733652   \n",
      "12             0.731305           0.001673             0.721473   \n",
      "13             0.721036           0.001613             0.710425   \n",
      "14             0.711675           0.001648             0.700402   \n",
      "15             0.703144           0.001716             0.691269   \n",
      "16             0.695517           0.001821             0.683022   \n",
      "17             0.688811           0.001849             0.675621   \n",
      "18             0.682927           0.001915             0.669015   \n",
      "19             0.677678           0.002002             0.663054   \n",
      "20             0.672719           0.002315             0.657458   \n",
      "21             0.668380           0.002262             0.652431   \n",
      "22             0.664326           0.002220             0.647651   \n",
      "23             0.660785           0.001935             0.643412   \n",
      "24             0.657283           0.001767             0.639269   \n",
      "25             0.654003           0.001817             0.635269   \n",
      "26             0.651092           0.001786             0.631871   \n",
      "27             0.648451           0.001815             0.628522   \n",
      "28             0.646009           0.001962             0.625400   \n",
      "29             0.643814           0.002033             0.622527   \n",
      "..                  ...                ...                  ...   \n",
      "196            0.591535           0.002215             0.476818   \n",
      "197            0.591581           0.002163             0.476280   \n",
      "198            0.591548           0.002180             0.475738   \n",
      "199            0.591576           0.002208             0.475350   \n",
      "200            0.591608           0.002273             0.474911   \n",
      "201            0.591502           0.002277             0.474420   \n",
      "202            0.591486           0.002336             0.473869   \n",
      "203            0.591428           0.002301             0.473352   \n",
      "204            0.591356           0.002205             0.472885   \n",
      "205            0.591369           0.002198             0.472349   \n",
      "206            0.591344           0.002183             0.471911   \n",
      "207            0.591340           0.002160             0.471484   \n",
      "208            0.591372           0.002143             0.471049   \n",
      "209            0.591274           0.002108             0.470609   \n",
      "210            0.591230           0.002155             0.470174   \n",
      "211            0.591210           0.002136             0.469731   \n",
      "212            0.591207           0.002059             0.469280   \n",
      "213            0.591187           0.002055             0.468766   \n",
      "214            0.591176           0.002093             0.468347   \n",
      "215            0.591191           0.002093             0.467890   \n",
      "216            0.591215           0.002145             0.467377   \n",
      "217            0.591192           0.002110             0.466926   \n",
      "218            0.591239           0.002080             0.466451   \n",
      "219            0.591296           0.002092             0.466008   \n",
      "220            0.591238           0.002029             0.465500   \n",
      "221            0.591173           0.002047             0.465068   \n",
      "222            0.591120           0.002082             0.464694   \n",
      "223            0.591146           0.002070             0.464289   \n",
      "224            0.591206           0.001984             0.463780   \n",
      "225            0.591111           0.001880             0.463306   \n",
      "\n",
      "     train-mlogloss-std  \n",
      "0              0.000608  \n",
      "1              0.000568  \n",
      "2              0.000594  \n",
      "3              0.000352  \n",
      "4              0.000365  \n",
      "5              0.000166  \n",
      "6              0.000291  \n",
      "7              0.000342  \n",
      "8              0.000428  \n",
      "9              0.000466  \n",
      "10             0.000458  \n",
      "11             0.000231  \n",
      "12             0.000227  \n",
      "13             0.000290  \n",
      "14             0.000357  \n",
      "15             0.000334  \n",
      "16             0.000191  \n",
      "17             0.000241  \n",
      "18             0.000300  \n",
      "19             0.000313  \n",
      "20             0.000238  \n",
      "21             0.000135  \n",
      "22             0.000123  \n",
      "23             0.000322  \n",
      "24             0.000416  \n",
      "25             0.000520  \n",
      "26             0.000525  \n",
      "27             0.000505  \n",
      "28             0.000444  \n",
      "29             0.000502  \n",
      "..                  ...  \n",
      "196            0.000875  \n",
      "197            0.000934  \n",
      "198            0.000903  \n",
      "199            0.000894  \n",
      "200            0.000962  \n",
      "201            0.000984  \n",
      "202            0.000883  \n",
      "203            0.000865  \n",
      "204            0.000839  \n",
      "205            0.000798  \n",
      "206            0.000741  \n",
      "207            0.000762  \n",
      "208            0.000722  \n",
      "209            0.000726  \n",
      "210            0.000799  \n",
      "211            0.000710  \n",
      "212            0.000740  \n",
      "213            0.000780  \n",
      "214            0.000762  \n",
      "215            0.000784  \n",
      "216            0.000738  \n",
      "217            0.000674  \n",
      "218            0.000681  \n",
      "219            0.000680  \n",
      "220            0.000758  \n",
      "221            0.000847  \n",
      "222            0.000881  \n",
      "223            0.000863  \n",
      "224            0.000912  \n",
      "225            0.000913  \n",
      "\n",
      "[226 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "xgb2_3 = XGBClassifier(\n",
    "          learning_rate =0.1,\n",
    "        n_estimators=2000, \n",
    "        max_depth=6,       #第一轮参数调整得到的最优值\n",
    "        min_child_weight=1, #第一轮参数调整得到的最优值\n",
    "        gamma=0,\n",
    "        subsample=0.3,\n",
    "        colsample_bytree=0.8,\n",
    "        colsample_bylevel = 0.7,\n",
    "        reg_alpha= 2, #第一轮参数调整得到的最优值\n",
    "        reg_lambda=0.5,#第一轮参数调整得到的最优值\n",
    "        objective= 'multi:softprob',\n",
    "        seed=3)\n",
    "\n",
    "modelfit(xgb2_3, X_train, y_train, cv_folds = kfold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 重新调整后弱学习器的最佳数目为226"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
